package umbook.web;

import java.io.IOException;
import java.util.List;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import umbook.domain.Comentarios;
import umbook.domain.LogItem;
import umbook.domain.Muro;
import umbook.domain.Usuario;
import umbook.service.ServiceMuro;
import umbook.service.ServiceUsuario;

@RequestMapping("/index/**")
@Controller
public class IndexController {

    @RequestMapping(method = RequestMethod.POST, value = "{id}")
    public void post(@PathVariable Long id, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {

    }

    @RequestMapping
    public String index() {
        return "index/index";
    }
    @RequestMapping(value = "/addUsuario", method = RequestMethod.POST )
    public String addUsuario(@RequestParam(value="nombre",required=false) String nombre, 
    		@RequestParam(value="apellido",required=false) String apellido,
    		@RequestParam(value="mail",required=false) String mail,
    		@RequestParam(value="pass",required=false) String pass,
    		@RequestParam(value="birthDate",required=false) String birthDate,
    		@RequestParam(value="imagen",required=false) Byte[] imagen,
    		@RequestParam(value="genero",required=false) String genero,
    		Model uiModel, HttpServletRequest request,HttpServletResponse response) throws IOException {
    	
    	boolean respuesta= new ServiceUsuario().registrarUsuario(nombre, apellido, mail, pass, birthDate, imagen, genero);
    	
    	if(respuesta){
    		Usuario usuario= new ServiceUsuario().loguearUsuario(mail, pass);
    		
    		LogItem imagenId=new ServiceMuro().recuperarImagen(usuario);
    		if (imagenId!=null){
    			uiModel.addAttribute("imagenId", imagenId.getId());
    		}else{
    			uiModel.addAttribute("imagenId", 1);
    		}
    		uiModel.addAttribute("usuario", usuario);
 
    		return "home/index";
    		
    	}else{
    		return "error/index";
    	}
        
    }
    
    @RequestMapping(value = "/login", method = RequestMethod.POST )
    public String login(@RequestParam(value="mail",required=false) String mail,
    		@RequestParam(value="pass",required=false) String pass,
    		Model uiModel, HttpServletRequest request,HttpServletResponse response) throws IOException{
    	Usuario usuario= new ServiceUsuario().loguearUsuario(mail, pass);
    	
    	if(usuario==null){
    		return "error/errorLogin";
    		
    	}else{
    		
    		List<Comentarios> com= new ServiceMuro().recuperarComentarios(usuario);
    		Muro m = new ServiceMuro().recuperarMuro(usuario);
    		LogItem imagenId=new ServiceMuro().recuperarImagen(usuario);
    		if (imagenId!=null){
    			uiModel.addAttribute("imagenId", imagenId.getId());
    		}else{
    			uiModel.addAttribute("imagenId", 1);
    		}
    		uiModel.addAttribute("comentarios", com);
    		uiModel.addAttribute("usuario", usuario);
    		uiModel.addAttribute("muro", m);
 
    		
    		return "home/index";
    	}
    }
   
}
